<?php
/*
Plugin Name: Pin This
Version: 1.0
Plugin URI: http://bluewearld.wordpress.com
Author: Bluewearld
Author URI: http://bluewearld.wordpress.com
Description: Enable your content to be pinnable for Pinterest.
*/
?>
<?php
if (!function_exists('add_action')) { header('Status: 403 Forbidden'); header('HTTP/1.1 403 Forbidden'); exit("This page can't be loaded outside of WordPress!"); }
global $wp_version;
if (version_compare($wp_version, "3.5.1", "<")) { exit('<b>Pin This</b> requires WordPress 3.5.1 OR newer. <a href="http:/codex.wordpress.org/Upgrading_WordPress">Please update!</a>'); }
if(!class_exists('PinThis')):
define('PinThis_URLPATH', WP_PLUGIN_URL.'/'.plugin_basename(dirname(__FILE__)));
class PinThis {
	
	var $plugin_name = 'Pin This';
	var $general_settings = 'pinthis_general_settings';
	
	function cisto( $v ) { $v=strip_tags($v); $v=stripslashes($v); $v=trim($v); return $v; }
	
	/* Back-end functions */
	
	function initial_install() { add_option( @$this->general_settings, array('btn_size'=>"large") ); }
	function on_admin_menu() {
		$this->pagehook = add_menu_page(@$this->plugin_name, @$this->plugin_name, 7, 'pinthis', array(&$this, 'on_show_page'), PinThis_URLPATH.'/pinthis-icon.png');
		add_action('load-'.$this->pagehook, array(&$this, 'on_load_page'));
	}
	function on_load_page() {
		wp_enqueue_script('common'); wp_enqueue_script('wp-lists'); wp_enqueue_script('postbox');
		add_meta_box('panel_info', 'Information', array(&$this, 'panel_info'), $this->pagehook, 'side', 'core');
		add_meta_box('panel_settings', 'General Settings', array(&$this, 'panel_settings'), $this->pagehook, 'normal', 'core');
	}
	function on_show_page() {
		?>
<div id="pinthis_panels" class="wrap">
    <?php screen_icon('options-general'); ?>
    <h2><?=@$this->plugin_name;?></h2>
    <div id="poststuff" class="metabox-holder has-right-sidebar">
        <div id="side-info-column" class="inner-sidebar">
            <?php do_meta_boxes($this->pagehook, 'side', ''); ?>
        </div>
        <div id="post-body" class="has-sidebar">
            <div id="post-body-content" class="has-sidebar-content">
                <?php do_meta_boxes($this->pagehook, 'normal', ''); ?>
                <br class="clear"/>
            </div>
        </div>
        <br class="clear"/>
    </div>	
</div>
<script type="text/javascript">
    //<![CDATA[
    jQuery(document).ready(function($) { $('.if-js-closed').removeClass('if-js-closed').addClass('closed'); postboxes.add_postbox_toggles('<?=$this->pagehook;?>'); });
    //]]>
</script>
		<?php
	}
	function panel_settings() {
		$h = "pinthis_settings_form";
		$msg = NULL;
		if (isset($_POST[$h])) {
			$btn_size = @$this->cisto($_POST["pinthis_button_size"]);
			if ($btn_size){
				update_option(@$this->general_settings, array('btn_size'=>$btn_size));
				$msg = '<div id="message" class="updated fade"><p><strong>! SUCCESS:</strong>&nbsp;General settings are updated</p></div>';
			}
			else {	
				$msg = '<div id="msg_error" class="updated fade"><p><strong>! ERROR:</strong>&nbsp;Please make a selection before saving settings</p></div>';
			}
		}
		$settings = get_option(@$this->general_settings);
		echo $msg;
		?>
        <style type="text/css">div#msg_error.updated, .login .message { background-color: #D6A3A3; border-color: #7C4646; }</style>
        <form action="" method="post">
        <input type="hidden" name="<?=$h;?>" value="1" />
		<p>Set whether you'd like to have the large or small Pinterest button appear over your images.</p>
        <div style="margin:0 0 20px 0;">
        	<label for="pinthis_button_size_large">
            	<div style="padding:20px 10px 0 0; float:left;"><input type="radio" id="pinthis_button_size_large" name="pinthis_button_size" value="large" <?=("large"==$settings['btn_size']) ? 'checked="checked"' : NULL;?> /></div>
                <div style="float:left;"><img src="<?=PinThis_URLPATH."/pinthis-btn-large.png";?>" /></div>
                <div style="padding:20px 0 0 10px; float:left;">Large</div>
                <div style="clear:both;"></div>
            </label>
        </div>
        <div style="margin:0 0 20px 0;">
        	<label for="pinthis_button_size_small">
            	<div style="padding:0 10px 0 0; float:left;"><input type="radio" id="pinthis_button_size_small" name="pinthis_button_size" value="small" <?=("small"==$settings['btn_size']) ? 'checked="checked"' : NULL;?> /></div>
                <div style="float:left;"><img src="<?=PinThis_URLPATH."/pinthis-btn-small.png";?>" /></div>
                <div style="padding:0 0 0 10px; float:left;">Small</div>
                <div style="clear:both;"></div>
            </label>
        </div>
        <div><?php submit_button("Save Settings",'primary','submit',false); ?></div>
        </form>
		<?php
	}
	function panel_info() {
		?>
        <h2>About Pin This Plugin</h2>
		<p>This plugin is intended to allow for all images on your website to be "<em>pinnable</em>", which means to post images to <a href="http://pinterest.com?rf=pinthis" target="_blank">Pinterest</a>.  When a visitor to your website hovers over an image the Pinterest icon will appear allowing for that image to be "<em>pinned</em>".</p>
		<?php
	}
	/* Front-end scripts */
	function wp_head() {
		?>
<style type="text/css">
.pinthis_images img { position:absolute; }
	.pinthis_images img.btn { padding:0; border:none; background:none; }
.pinthis_images .pinthis_images_link { position:relative; display:none;  }
.pinthis_images .btn { margin:5px; }
</style>
		<?php
	}
	function wp_footer() {
		$settings = get_option(@$this->general_settings);
		$pinthis_btn = PinThis_URLPATH."/pinthis-btn-{$settings['btn_size']}.png";
		?>
<script>
var pinthis_button="<?=$pinthis_btn;?>";

function pinthis_clean_url(e){b=e.toString().split("?");if(b.length>=2){return b[0]}else{return e}}

function pinthis_is_url(e){
	source=(e||"").toString();
	var t;
	regexToken=/(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g;
	r=false;
	while((t=regexToken.exec(source))!==null){token=t[0];r=token}return r
}

function pinthis_modal(e){window.open(e.href,"!Pin It","width=600,height=600");return false}var pinthis_url_pathname=pinthis_clean_url(window.location);jQuery(document).ready(function(){jQuery("img").wrap('<div class="pinthis_images"></div>');jQuery(".pinthis_images").append(function(e,t){imgurl=jQuery(t).attr("src");d=jQuery(t).attr("title");if(d!="undefined"||d==null||d==""){d=""}else{d="&description="+encodeURIComponent(d)}if(!pinthis_is_url(imgurl))imgurl=pinthis_url_pathname+imgurl;return'<a href="http://pinterest.com/pin/create/button/?url='+encodeURIComponent(pinthis_url_pathname)+"&media="+encodeURIComponent(imgurl)+d+'" class="pinthis_images_link pin-it-button" onclick="return pinthis_modal(this);"><img src="'+pinthis_button+'" class="btn" /></a>'}).hover(function(){jQuery(this).find("a").show("slow")},function(){jQuery(this).find("a").hide("slow")})})
</script>
<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>
		<?php
	}
	function jquery() {	if (!is_admin()) { wp_enqueue_script("jquery"); /*wp_enqueue_script('pinterest', 'http://assets.pinterest.com/js/pinit.js', array('jquery'));*/ }}
}
$PinThis = new PinThis();
else :
	exit("Class 'PinThis' already exists");
endif;
if (isset($PinThis)) {
	add_action('admin_menu', array(&$PinThis, 'on_admin_menu'));
	add_action('wp_head', array(&$PinThis, 'wp_head'));
	add_action('wp_footer', array(&$PinThis, 'wp_footer'));
	add_action('wp_print_scripts', array(&$PinThis, 'jquery'));
	if (is_admin()) { register_activation_hook(__FILE__, array(&$PinThis, 'initial_install')); }
}
?>